package com.ruoyi.comprehensive.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.comprehensive.domain.DustPreventionExamine;
import com.ruoyi.comprehensive.domain.GasPreventionExamine;
import com.ruoyi.comprehensive.domain.PreventionEnclosure;
import com.ruoyi.comprehensive.dto.DustPreventionDto;
import com.ruoyi.comprehensive.mapper.PreventionEnclosureMapper;
import com.ruoyi.comprehensive.service.IDustPreventionExamineService;
import com.ruoyi.comprehensive.vo.DustVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.comprehensive.mapper.DustPreventionMapper;
import com.ruoyi.comprehensive.domain.DustPrevention;
import com.ruoyi.comprehensive.service.IDustPreventionService;

/**
 * 防尘管理Service业务层处理
 * 
 * @author hcc
 * @date 2023-12-15
 */
@Service
public class DustPreventionServiceImpl implements IDustPreventionService 
{
    @Autowired
    private DustPreventionMapper dustPreventionMapper;
    @Autowired
    private PreventionEnclosureMapper preventionEnclosureMapper;
    @Autowired
    private IDustPreventionExamineService dustPreventionExamineService;
    /**
     * 查询防尘管理
     * 
     * @param id 防尘管理主键
     * @return 防尘管理
     */
    @Override
    public DustPrevention selectDustPreventionById(String id)
    {
        return dustPreventionMapper.selectDustPreventionById(id);
    }

    /**
     * 查询防尘管理列表
     * 
     * @param dustPrevention 防尘管理
     * @return 防尘管理
     */
    @Override
    public List<DustPrevention> selectDustPreventionList(DustPrevention dustPrevention)
    {
        return dustPreventionMapper.selectDustPreventionList(dustPrevention);
    }

    /**
     * 新增防尘管理
     * 
     * @param dustPrevention 防尘管理
     * @return 结果
     */
    @Override
    public int insertDustPrevention(DustPrevention dustPrevention)
    {
        DustPrevention dustPrevention1 = new DustPrevention();
        BeanUtils.copyProperties(dustPrevention,dustPrevention1);
        dustPrevention1.setExamineStatus(0L);
        dustPrevention1.setCreateByName(SecurityUtils.getUsername());
        dustPrevention1.setCreateBy(SecurityUtils.getUserId().toString());
        dustPrevention1.setCreateAt(new Date());
        dustPrevention1.setUpdateAt(new Date());
        dustPrevention1.setUpdateByName(SecurityUtils.getUsername());
        dustPrevention1.setUpdateBy(SecurityUtils.getUserId().toString());
        return dustPreventionMapper.insertDustPrevention(dustPrevention1);
    }

    /**
     * 修改防尘管理
     * 
     * @param dustPrevention 防尘管理
     * @return 结果
     */
    @Override
    public int updateDustPrevention(DustPrevention dustPrevention)
    {
        if (dustPrevention.getExamineStatus() != null && dustPrevention.getExamineStatus() == 1L){
            DustPrevention dustPrevention1 = dustPreventionMapper.selectDustPreventionById(dustPrevention.getId());
            if (dustPrevention1 != null){
                DustPreventionExamine dustPreventionExamine = new DustPreventionExamine();
                BeanUtils.copyProperties(dustPrevention1,dustPreventionExamine);
                dustPreventionExamine.setDustPreventionId(dustPrevention1.getId());
                dustPreventionExamine.setExamineStatus(1L);
                dustPreventionExamineService.insertDustPreventionExamine(dustPreventionExamine);
            }
        }
        return dustPreventionMapper.updateDustPrevention(dustPrevention);
    }

    /**
     * 批量删除防尘管理
     * 
     * @param ids 需要删除的防尘管理主键
     * @return 结果
     */
    @Override
    public int deleteDustPreventionByIds(String[] ids)
    {
        return dustPreventionMapper.deleteDustPreventionByIds(ids);
    }

    /**
     * 删除防尘管理信息
     * 
     * @param id 防尘管理主键
     * @return 结果
     */
    @Override
    public int deleteDustPreventionById(String id)
    {
        return dustPreventionMapper.deleteDustPreventionById(id);
    }

    /**
     * 查询防尘管理列表
     * @param dustPrevention
     * @return
     */
    @Override
    public List<DustPreventionDto> getDustPreventionList(DustPrevention dustPrevention) {
        List<DustPreventionDto> dustPreventionList = dustPreventionMapper.getDustPreventionList(dustPrevention);
        return dustPreventionList;
    }

    /**
     * 获取防尘管理详细信息
     * @param id
     * @return
     */
    @Override
    public DustPreventionDto getDustPreventionById(String id) {
        DustPreventionDto dustPreventionDto = new DustPreventionDto();
        DustPrevention dustPrevention = dustPreventionMapper.getDustPreventionById(id);
        BeanUtils.copyProperties(dustPrevention,dustPreventionDto);
        PreventionEnclosure preventionEnclosure = new PreventionEnclosure();
        preventionEnclosure.setPreventionId(dustPrevention.getId());
        preventionEnclosure.setEnclosureType("3");
        // 查询附件信息内容
        List<PreventionEnclosure> preventionEnclosures = preventionEnclosureMapper.selectPreventionEnclosureList(preventionEnclosure);
        dustPreventionDto.setEnclosures(preventionEnclosures);
        return dustPreventionDto;
    }

}
